<?php

namespace app\model\v1\repair;

use app\model\order\StoreOrder;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;

class Maintenance extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    public function order()
    {
        return $this->hasOne (StoreOrder::class,'order_id','order_id');
    }


    /**
     * 维修管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getMaintenanceList($param = [])
    {
        $data["data"] = self::field ("*,
            case when audit_state=1 then '通过' 
                when audit_state=2 then '不通过' 
                when audit_state=0 then '待审核' 
                end as audit_text,
            case when maintenance_type=1 then '质保外' 
                when maintenance_type=2 then '质保内' 
                when maintenance_type=3 then '装修质保' 
                end as type_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('add_time', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("notes",'like', '%'.$param["word"].'%');
            })
            ->with([
                'order' => function ($query) {
                    $query->field("id,order_id,uid,real_name,user_phone");
                },
            ])
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('add_time', 'between', [$wheretime[0],  $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("notes",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }


    /**
     * 维修管理列表 - 查看
     * @param $param
     * @return Maintenance|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function detailMaintenance($param = [])
    {
        $detail = self::field ("*,
            case when audit_state=1 then '通过' 
                when audit_state=2 then '不通过' 
                when audit_state=0 then '待审核' 
                end as audit_text,
            case when maintenance_type=1 then '质保外' 
                when maintenance_type=2 then '质保内' 
                when maintenance_type=3 then '装修质保' 
                end as type_text
            ")
            ->where("id", $param["id"])
            ->with([
                'order' => function ($query) {
                    $query->field("id,order_id,uid,real_name,user_phone");
                },
            ])
            ->find();

        return $detail;
    }



    /**
     * 维修管理列表 - 审核按钮
     * @param $param
     * @return RecruitCompany
     */
    public static function auditMaintenance($param = [])
    {
        $update = ['audit_state' => $param["audit_state"]];
        if(!empty($param["reason"])) $update["reason"] = $param["reason"];

        $res = self::where("id", $param['id'])->update($update);
        return $res;
    }


}